<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ include file="/common/taglibs.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>修改系统用户</title>
<link type="text/css" rel="stylesheet"
	href="${ctx }/js/ztree/css/zTreeStyle/zTreeStyle.css" />
<script type="text/javascript" src="${ctx }/js/bootstrap/jquery.js"></script>
<script type="text/javascript">
	$(function() {
		
		jQuery.fn.selectDept = function(targetId) {
			var _seft = this;
			var targetId = $(targetId);
		
			this.click(function(){
				var A_top = $(this).offset().top + $(this).outerHeight(true)-45;
				var A_left =  $(this).offset().left;
				targetId.show().css({"position":"absolute","top":A_top+"px" ,"left":A_left+"px"});
			});
		
			$(document).click(function(event){
				if(event.target.id!=_seft.selector.substring(1)){
					targetId.hide();	
				}
			});
		
			targetId.click(function(e){
				e.stopPropagation(); //  2
			});
		
		    return this;
		}
	
		$("#deptName").selectDept("#win-box");
		
		$("#province").val('${entity.province}');
		getCitys('${entity.province}');
		$("#city").val('${entity.city}');
		getMunicipality('${entity.city}');
		$("#municipality").val('${entity.municipality}');
	});
	
	var setting = {
			view: {
				showIcon: false
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				onClick: addOrEditMenu
			}
		};
			
		var zNodes = ${deptTree};

		var zTree;

		$(document).ready(function(){
			$.fn.zTree.init($("#deptTree"), setting, zNodes);
			zTree = $.fn.zTree.getZTreeObj("deptTree");
		});

		function addOrEditMenu(event, treeId, treeNode, clickFlag){
			$("#deptName").val(treeNode.name);
			$("#deptCode").val(treeNode.id);
			$("#win-box").hide();
			$('.deptNameformError').hide();
		}
	
	$(function() {
		var leftSel = $("#selectL");
		var rightSel = $("#selectR");
		leftSel.dblclick(function() {
			$(this).find("option:selected").each(function() {
				$(this).remove().appendTo(rightSel);
			});
		});
		rightSel.dblclick(function() {
			$(this).find("option:selected").each(function() {
				$(this).remove().appendTo(leftSel);
			});
		});
		
		var th = $(".row");
	    th.on("click",".isb-L,.isb-R",function(){
	   	   var self = $(this);
	   	   /*左侧点击*/
	
	   	   (self.hasClass("isb-L"))&&(function(){
	
	            var sel = selected("L","R");
	   	   }());
	   	   /*右侧*/
	   	   (self.hasClass("isb-R"))&&(function(){
	
	            var sel = selected("R","L");
	   	   }());
	   });
	   function selected(_form,_to){
	   		var sec = $(".iss-SB-"+_form).find("select option"),
	   		    tos = $(".iss-SB-"+_to).find("select");
	         sec.each(function(ind,ele){
	         	(ele.selected)&&(tos.append(ele));
	         	 
	         })
	   }
	});
	
	
	function submitForm(){
		if($("#mainForm").validationEngine('validate')){
			var selVal = [];
			$("#selectR").find("option").each(function() {
				selVal.push(this.value);
			});
			var selVals = selVal.join(",");
			$("#roleValueId").val(selVals);
			if(selVals == ""){
				art.dialog({content:"您未给该系统用户赋角色，确认提交吗？",ok:function(){sub();},cancel:true});
			}else{
				sub();
			}
		}
	}
	
	function sub(){
		getRightsId();
		$("#mainForm").ajaxSubmit({
	        type: "post",
	        dataType: "text",
	        success: function(data){
	        	if(data == 'true'){
	        		art.dialog({content:"保存成功！",ok:function(){window.location.href="${ctx}/user/list";}});
	        	}else{
	        		art.dialog({content:"保存失败！",ok:true});
	        	}
	        }
	    });
	}
	
	function addRight(){
		art.dialog.open("${ctx }/project/rights/selectRights", {
			title: '知识产权信息列表', 
			width: 1040, 
			height: 550,
			close: function(){
					var data = art.dialog.data("json");
					
					if(data != undefined && data != ""){
						var jsonDate = eval("("+data+")");
						var trs = "";
						for(var i=0;i<jsonDate.length;i++){
							trs += "<tr>"+
										"<td>"+jsonDate[i].code+"<input type='hidden' name='rightsId' value='"+jsonDate[i].id+"' /></td>"+
										"<td>"+jsonDate[i].name+"</td>"+
										"<td>"+jsonDate[i].applicant+"</td>"+
										"<td>"+jsonDate[i].mobile+"</td>"+
										"<td>"+jsonDate[i].status+"</td>"+
										"<td><a href='javascript:void(0);' onclick='deleteTr(this)'>删除</a></td>"+
									"</tr>"
						}
						
						$("#rightsTable").append(trs);
						$("#rightsTable").show();
					}
					
					art.dialog.data("json","");
					
					
		    	}
		}, false);
	}
	
	function deleteTr(Obj){
		$(Obj).parent().parent().remove();
	}
	
	function getRightsId(){
		var rightsIds = $("input[name='rightsId']");
		var postValue = "";
		$(rightsIds).each(function(index,element){
			if(index == 0){
				postValue += $(this).val();
			}else{
				postValue += ","+$(this).val();
			}
		});
		
		$("#rightsValueId").val(postValue);
		return postValue;
	}
</script>
</head>
<body>
	<div class="breadcrumbs" id="breadcrumbs">
		<script type="text/javascript">
        try{ace.settings.check('breadcrumbs' , 'fixed')}catch(e){}
    </script>

		<ul class="breadcrumb">
			<li><i class="ace-icon fa fa-home home-icon"></i> <a
				href="${ctx }/home/home">首页</a></li>
			<li class="active">系统管理</li>
			<li class="active">用户管理</li>
		</ul>
		<!-- .breadcrumb -->
	</div>
	<div class="page-content">
		<div class="page-header">
			<h1>
				用户管理 <small> <i class="ace-icon fa fa-double-angle-right"></i> 修改用户
				</small>
			</h1>
		</div>
		<!-- /.page-header -->
		<form class="form-horizontal" id="mainForm"
			action="${ctx }/user/update" method="post">
			<input type="hidden" id="id" name="id" value="${entity.id }" /> <input
				type="hidden" id="roleValueId" name="roleValueId" /> <input
				type="hidden" id="rightsValueId" name="rightsValueId" />
			<div class="row">
				<div class="col-xs-12">
					<div class="col-sm-6">
						<div>
							<div class="form-group">
								<label for="inputEmail3" class="col-sm-4 control-label"><b
									class="red">*</b><strong>用户账号</strong></label>
								<div class="col-sm-8">
									<%-- <input type="text" class="form-control validate[required]" id="userCode"  name="userCode" maxlength="20" value="${user.userCode }"> --%>
									<p class="form-control-static">${entity.userCode }</p>
								</div>
							</div>
							<div class="form-group">
								<label for="inputPassword3" class="col-sm-4 control-label"><strong>手机号码</strong></label>
								<div class="col-sm-8">
									<input type="text"
										class="form-control validate[custom[mobile],minSize[11],maxSize[11]]"
										id="mobile" name="mobile" maxlength="11"
										value="${entity.mobile }">
								</div>
							</div>
							<div class="form-group">
								<label for="inputPassword3" class="col-sm-4 control-label"><b
									class="red">*</b><strong>部门</strong></label>
								<div class="col-sm-8">
									<input type="text" id="deptName"
										value="<c:out value="${entity.department.name }" escapeXml="true"></c:out>" class="form-control"
										readonly="readonly" /> <input type="hidden" id="deptCode"
										name="deptId" value="" />
								</div>
							</div>
							<div class="form-group">
								<label for="inputEmail3" class="col-sm-4 control-label"><strong>性别</strong></label>
								<div class="col-sm-8">
									<select:option type="sex" id="sex" name="sex" showValue="false"
										styleClass="form-control" value="${entity.sex }" />
								</div>
							</div>
							<div class="form-group">
								<label for="inputEmail3" class="col-sm-4 control-label"><b
									class="red">*</b><strong>所属地区</strong></label>
								<div class="col-sm-8">
									<tags:areaSelect provinceList="${provinceList}" styleClass="validate[required]" provinceName="province" cityName="city" municipalityName="municipality" type="3"  />
								</div>
							</div>
						</div>
					</div>
					<div class="col-sm-6">
						<div>
							<div class="form-group">
								<label for="inputEmail3" class="col-sm-4 control-label"><b
									class="red">*</b><strong>用户姓名</strong></label>
								<div class="col-sm-8">
									<input type="text" class="form-control validate[required]"
										id="name" name="name" maxlength="20" value="<c:out value="${entity.name }" escapeXml="true"></c:out>">
								</div>
							</div>
							<div class="form-group">
								<label for="inputPassword3" class="col-sm-4 control-label"><strong>邮箱</strong></label>
								<div class="col-sm-8">
									<input type="text" class="form-control validate[custom[email]]"
										id="email" name="email" maxlength="30"
										value="${entity.email }">
								</div>
							</div>
							<div class="form-group">
								<label for="inputPassword3" class="col-sm-4 control-label"><strong>座机</strong></label>
								<div class="col-sm-8">
									<input type="text" class="form-control" id="telephone"
										name="telephone" maxlength="20" value="<c:out value="${entity.telephone }" escapeXml="true"></c:out>">
								</div>
							</div>
							<%-- <div class="form-group">
								<label for="inputEmail3" class="col-sm-4 control-label"><b
									class="red">*</b><strong>用户身份</strong></label>
								<div class="col-sm-8">
									<select:option type="identity" id="identity" name="identity" showValue="false"
										styleClass="form-control validate[required]" blank="true" value="${entity.identity }"/>
								</div>
							</div> --%>
							<div class="form-group">
								<label for="manufactorCode" class="col-sm-4 control-label"><strong>厂商</strong></label>
								<div class="col-sm-8">
									<select id="manufactorCode" name="manufactorCode" class="form-control">
										<option value="">请选择</option>
										<c:forEach items="${manufactorList }" var="mf">
											<option value="${mf.code }" <c:if test="${mf.code==entity.manufactorCode }">selected="selected"</c:if> >${mf.name }</option>
										</c:forEach>
									</select>
								</div>
							</div>
						</div>
					</div>
				</div>
				<h3 class="header smaller lighter gray">角色选择</h3>
				<div class="col-xs-5 iss-SB-L">
					<h4>可选角色</h4>
					<select multiple class="form-control" style="height: 200px;"
						id="selectL">
						<c:forEach items="${unownedRole }" var="role">
							<option value="${role.id }" title="${role.rmk }">${role.roleName }</option>
						</c:forEach>
					</select>
				</div>
				<div class="col-xs-2">
					<div style="margin-top: 40px;">
						<button type="button"
							class="btn btn-default btn-block btn-sm isb-L">&gt;&gt;</button>
						<button type="button"
							class="btn btn-default btn-block btn-sm isb-R">&lt;&lt;</button>
					</div>
				</div>
				<div class="col-xs-5 iss-SB-R">
					<h4>已选角色</h4>
					<select multiple class="form-control" style="height: 200px;"
						id="selectR">
						<c:forEach items="${entity.roles }" var="role">
							<option value="${role.id }" title="${role.rmk }">${role.roleName }</option>
						</c:forEach>
					</select>
				</div>
				
				
				<div class="col-xs-12" style="text-align: center; margin-top: 20px;">
					<button type="button" class="btn btn-info" onclick="submitForm()">保存</button>
					<button type="button" class="btn btn-default"
						onclick="javascript:window.location.href='${ctx}/user/list'">返回</button>
				</div>
			</div>
		</form>
	</div>
	<div class="col-xs-3" style="display: none; padding-left: 0px;"
		id="win-box">
		<div class="widget-box" style="margin-top: 0px;">
			<div class="widget-header header-color-blue2">
				<h4 class="lighter smaller">选择部门</h4>
			</div>
			<div class="widget-body">
				<div class="widget-main padding-8">
					<ul id="deptTree" class="ztree"></ul>
				</div>
			</div>
		</div>
	</div>
</body>
</html>